/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.alibaba.ververica.cdc.connectors.mysql;

import org.apache.flink.test.util.AbstractTestBase;

import com.alibaba.ververica.cdc.connectors.mysql.utils.MySQLContainer;
import org.junit.BeforeClass;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.containers.output.Slf4jLogConsumer;
import org.testcontainers.lifecycle.Startables;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.stream.Stream;

/**
 * Basic class for testing MySQL binlog source, this contains a MySQL container which enables binlog.
 */
public abstract class MySQLTestBase extends AbstractTestBase {

	private static final Logger LOG = LoggerFactory.getLogger(MySQLTestBase.class);

	protected static final MySQLContainer MYSQL_CONTAINER = (MySQLContainer) new MySQLContainer()
		.withConfigurationOverride("docker/my.cnf")
		.withSetupSQL("docker/setup.sql")
		.withDatabaseName("flink-test")
		.withUsername("flinkuser")
		.withPassword("flinkpw")
		.withLogConsumer(new Slf4jLogConsumer(LOG));

	@BeforeClass
	public static void startContainers() {
		LOG.info("Starting containers...");
		Startables.deepStart(Stream.of(MYSQL_CONTAINER)).join();
		LOG.info("Containers are started.");
	}

	protected Connection getJdbcConnection() throws SQLException {
		return DriverManager.getConnection(
			MYSQL_CONTAINER.getJdbcUrl(),
			MYSQL_CONTAINER.getUsername(),
			MYSQL_CONTAINER.getPassword());
	}
}
